#include "../vcd-common-api/vcd-common.h"
#include "../vcd-common-api/vcd-conf/vcd-conf.h"
#include "vcd-message-dialog.h"
#include "vcd-passwd-window.h"

typedef struct _AskContext{
    GtkWidget *ask_window;
    GtkWidget *entry;
    PasswdContext* pass_ctx;
}AskContext;

static void click_yes(AskContext* ctx);
static void click_cancel(AskContext* ctx);

static void click_yes(AskContext* ctx)
{
    if(ctx == NULL || ctx->pass_ctx == NULL){
        return;
    }

    //get enter passwd
    const gchar* passwd = gtk_entry_get_text((GtkEntry *)ctx->entry);
    PasswdContext* pc = ctx->pass_ctx;

    //check passwd ok
    if(pc->check_func(passwd) == 0){
        //destroy passwd window
        gtk_widget_destroy(ctx->ask_window);
        pc->ok_func();
        g_free(ctx);
        ctx = NULL;
    }else{
        //passwd is wrong
        message_box("密码错误!");
    }
}

static void click_cancel(AskContext* ctx)
{
    if(ctx == NULL || ctx->pass_ctx == NULL){
        return;
    }

    gtk_widget_destroy(ctx->ask_window);
    g_free(ctx);
    ctx = NULL;
}

void get_sys_passwd_ui(PasswdContext* pc)
{
    struct vcd_conf* cf = g_vcd_conf;
    if (cf->sys_passwd == NULL || strlen(cf->sys_passwd) == 0) {
        pc->ok_func();
        return;
    }

    GtkWidget *ask_window, *fix, *label, *entry, *btn_yes, *btn_cancel;
    ask_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_keep_above(GTK_WINDOW(ask_window), TRUE);
    gtk_window_set_modal((GtkWindow *)ask_window, TRUE);
    gtk_window_set_position( GTK_WINDOW(ask_window), GTK_WIN_POS_CENTER_ALWAYS);//居中
    gtk_container_set_border_width(GTK_CONTAINER(ask_window), 25);
    gtk_window_set_decorated(GTK_WINDOW(ask_window), FALSE);//无边框

    btn_yes = gtk_button_new_with_label("确定");
    btn_cancel = gtk_button_new_with_label("取消");
    label = gtk_label_new("请输入密码:");

    fix   = gtk_fixed_new();
    entry = gtk_entry_new();

    gtk_button_set_relief(GTK_BUTTON(btn_cancel),  GTK_RELIEF_NONE);
    gtk_button_set_relief(GTK_BUTTON(btn_yes), GTK_RELIEF_NONE);
    gtk_widget_set_size_request(btn_yes, 100, -1);
    gtk_widget_set_size_request(btn_cancel, 100, -1);
    gtk_widget_set_size_request(entry, 210, -1);
    gtk_entry_set_visibility((GtkEntry *)entry, FALSE);

    gtk_fixed_put((GtkFixed *)fix, label,  0,   0);
    gtk_fixed_put((GtkFixed *)fix, entry,  0,   20);
    gtk_fixed_put((GtkFixed *)fix, btn_yes, 0,   60);
    gtk_fixed_put((GtkFixed *)fix, btn_cancel,  110, 60);

    gtk_container_add(GTK_CONTAINER(ask_window), fix);

    AskContext* ctx = g_malloc0(sizeof(AskContext));
    if(ctx == NULL) {
        gtk_widget_destroy(ask_window);
        ask_window = NULL;
        g_free(pc);
        pc = NULL;
        return;
    }

    ctx->entry = entry;
    ctx->ask_window = ask_window;
    ctx->pass_ctx = pc;

    g_signal_connect_swapped(G_OBJECT(btn_cancel),  "clicked", G_CALLBACK(click_cancel), ctx);
    g_signal_connect_swapped(G_OBJECT(btn_yes), "clicked", G_CALLBACK(click_yes), ctx);
    g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(click_yes), NULL);

    /*            CSS                  */
    set_css(btn_yes, "button{color: white;background: rgb(53, 126, 169);border: 1px solid rgb(40, 94, 142);border-radius: 0px}");
    set_css(btn_yes, "button:hover{border-radius: 0px;background: #3070a9}");
    set_css(btn_cancel, "button{color: white;background: #5cb85c;border: 1px solid rgb(57, 132, 57);border-radius: 0px}");
    set_css(btn_cancel, "button:hover{border-radius:0px;background: #449d44;}");
    set_css(ask_window, "window{border-radius:0px;background-color: rgb(245, 245, 245)}");
    /*            CSS                 */
    gtk_widget_show_all(ask_window);
    gtk_window_set_position( GTK_WINDOW(ask_window), GTK_WIN_POS_CENTER_ALWAYS);//居中
}
